package cn.ven.controller;


import cn.ven.entity.Largefile;
import cn.ven.entity.MyUser;
import cn.ven.service.LargeFileServiceImpl;
import cn.ven.service.UserServiceImpl;
import cn.ven.tool.Token;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;


@RestController
@RequestMapping("/api/info")
public class InfoController {
    private static Logger log = LoggerFactory.getLogger(InfoController.class);
    @Autowired
    UserServiceImpl userService;

    @Autowired
    LargeFileServiceImpl largeFileService;


    //获取用户信息
    @RequestMapping("/getUser")
    public Object getUser(){
        Object user = Token.getAccount();
        return user;
    }

    //显示用户头像
    @RequestMapping("/show/{account}")
    public  void show(HttpServletResponse response,
                      @PathVariable("account") String account){
        log.info("account===="+account);
        MyUser myUser = userService.findUserByAccount(account);
        log.info("user====="+myUser);
        if (myUser==null||myUser.getImg()==null)//判断书是否为空或者图片是否为空
            return;
        try {
            Largefile largeFile = largeFileService.findById((String) myUser.getImg());//把b.getPicture与picture.uuid关联起来，进行图片查询
            byte[] buf=(byte[]) largeFile.getContent();//向下转型
            response.setContentType("image/jpeg");//设置图片的格式
            FileCopyUtils.copy(buf, response.getOutputStream()); //复制到网页进行输出
        }catch (Exception e){
            log.info(String.valueOf(e));
        }
    }


}
